var CSP_BSF_CALLBARID = "101";       //标识是否是接续菜单
var CSP_CALLCONF = "431118103101";   //呼叫适配菜单ID
var CSP_CALLBAR = "CSPSC";           //接续事件前缀
var allMenus = new Array();          //存储所有的menu对象
var onDutyId = "704";                //上班菜单ID
var offDutyId = "705";               //下班菜单ID    
var CSP_SERVICE = "201";             //服务请求系统菜单
var CSP_BSF_SYSMENUID = "00";        //系统菜单ID
var CSP_BSF_TOOLID = "01";        //系统工具ID
var CSP_SERVICECONFIG = "431";       //业务配置菜单ID
var CSP_BSF_CCT_MODULEID = "102";   //标识是否是客户接触菜单
var CSP_BSF_CCT_CONFIGMENU = "431123";  //标识是否是客户接触配置菜单，配置菜单不涉及菜单状态更改
var CSP_DIVS = new Array();          //保存所有的菜单DIV对象

function menu(htmlObj,parentId,path) {
    this.htmlObj = htmlObj;          //触发菜单事件的HTML元素
    this.popup = null;               //菜单窗体
    this.show = false;               //标识是否显示
    this.menuDivs = new Array();     //保存每个动态生成的菜单DIV
    this.left = 0;                   //menu菜单的左边距离
    this.top = 0;                    //menu菜单的高度距离
    this.width = 217;                //menu菜单的宽度
    this.height = 0;                 //menu菜单的高度
    this.parentId = parentId;        //该菜单列表属于那个父菜单
    this.refreshFlag = false;        //是否需要进行刷新的接续状态标志
    
    this.path = path.substring(0,path.length-1);                //系统的相对路径
    
    //保存创建的menu对象
    allMenus[parentId] = this;
};

menu.prototype.showMenu = function(Obj,divObj) {    
    //计算显示区域的位置,如果不存在触发菜单事件的HTML元素则退出
    //记录客户端日志
	if(divObj != null && divObj.menuObj != null)
	{
	    var oString = "menuClick event " + divObj.menuObj.id + " :begin";
	}
	else
	{
	    var oString = "menuClick event:begin";
	}

	wClientLog(oString);
	
    if(Obj == null)
    {
        if(this.htmlObj == null)
        {
	        wClientLog("menuClick event:htmlObj is null");
            return fasle;
        }    
        var oNode = this.htmlObj;
        var iLeft = 0;                   //计算窗体位置left
        var iTop = 0;                    //计算窗体位置top
        
        while(oNode.tagName != "BODY") {
            iLeft += oNode.offsetLeft;
            iTop += oNode.offsetTop;
            oNode = oNode.parentNode;     
        }
        
        wClientLog("get menu left and top begin");
        this.left = iLeft + window.screenLeft;
        this.top = iTop + window.screenTop + this.htmlObj.offsetHeight;
        wClientLog("get menu left and top end");
    }
    else
    {
        wClientLog("get menu left and top begin");
        this.left = Obj.left + Obj.width - 7;
        this.top = Obj.top + 3;
        if(divObj != null)
        {
            this.top = this.top + divObj.offsetTop;
        }
        wClientLog("get menu left and top end");
    }
    //去掉Path末位
    /*
    if(path == null)
    {
        path = "";
    }
    else
    {
        path = path.substring(0,path.length-1);
        
    }
    */
    
    //判断菜单列表是否已经创建，如果创建则只显示，否则重新创建然后显示
    if(this.popup == null)
    {
	    wClientLog("menuClick event:init popup begin");
        this.initPopup(Obj);
        //创建不成功则退出
        if(this.popup == null)
        {
	        wClientLog("menuClick event:init popup failed");
            return;
        }
    }
    //如果属于接续菜单则需要刷新状态
    if(divObj != null && divObj.menuObj.moduleId == CSP_BSF_CALLBARID && divObj.menuObj.id != CSP_CALLCONF)
    {
	    wClientLog("menuClick event:refreshSatue begin");
        this.refreshStatue(divObj);
        wClientLog("menuClick event:refreshSatue end");
    }
    
    //刷新客户接触前台菜单
    if(divObj != null && divObj.menuObj.moduleId == CSP_BSF_CCT_MODULEID && divObj.menuObj.id != CSP_BSF_CCT_CONFIGMENU)
    {
	    wClientLog("menuClick event:refreshCctMenu begin");
        this.refreshCctMenu(divObj);
        wClientLog("menuClick event:refreshCctMenu end");
    }

	wClientLog("menuClick event:do popup show begin");
    this.popup.show(this.left,this.top,this.width,this.height);
    wClientLog("menuClick event:do popup show end");
    wClientLog("menuClick event:end");
};

menu.prototype.initPopup = function(Obj) {
    //如果菜单列表已经存在则不需初始化
    if(this.popup != null)
    {
        wClientLog("the popup is not null,needn't to create new");
        return false;
    }
    
    //如果不存在父ID则退出
    if(this.parentId == null)
    {
        wClientLog("error:parentId is null");
        return false;
    }
    //初始化菜单操作
    var tmpMenus = top.publicObject["menu"];  
    if(tmpMenus != null)
    {
        //筛选当前需要打开的菜单项集合
        var childMenus = new Array();
        wClientLog("get child menus begin");
        for(var i=0;i<tmpMenus.length;i++)
        {
            //displayType为0或者为null表示显示该菜单，其他表示不显示
            if(tmpMenus[i].parentId == this.parentId && (tmpMenus[i].displayType=="0" || tmpMenus[i].displayType==null || tmpMenus[i].displayType=="null"))
            {
                childMenus.push(tmpMenus[i]);
            }
        }
        wClientLog("get child menus end");
        //如果不存才当前系统的子菜单，退出操作
        if(childMenus.length === 0)
        {
            wClientLog("error:no child menus");
            return false;
        }
        else
        {
            //菜单排序操作
            childMenus.sort(this.compareFunc);
        }
    }
    else
    {
        wClientLog("menu in topPublic is null");
    }
    
    //生成popup对象以及菜单DIV
    
    //由于popup子菜单显示会使父菜单消失,这里使用parentWindow进行创建,避免父菜单消失
    wClientLog("create popup begin");
    if(Obj!= null)
    {
        this.popup = Obj.popup.document.parentWindow.createPopup();
    }
    else
    {
        this.popup = window.createPopup();
    }
    wClientLog("create popup end");
    
    wClientLog("create div object begin");
    var oFragment = this.popup.document.createDocumentFragment();
    //动态创建菜单DIV
    for(var i=0;i<childMenus.length;i++)
    {
        var oDiv = this.popup.document.createElement("DIV");
        oDiv.id = childMenus[i].id;
        oDiv.menuUrl = childMenus[i].moduleId == CSP_BSF_CALLBARID ? childMenus[i].menuUrl + "()" : childMenus[i].menuUrl;
        oDiv.style.width = 217;
        oDiv.disabled = childMenus[i].disabled;
        oDiv.style.border = "0px solid #A0B1C1"; 
        oDiv.style.fontSize = "0"; 
        oDiv.isCallBarItem = childMenus[i].isCallBarItem;
        if(childMenus[i].isCallBarItem.indexOf("H")>-1)
        {
            oDiv.style.backgroundColor = "#98B9E7";
        }
        else
        {
            oDiv.style.backgroundColor = "#EBF0F4";
        }
        
        //添加到所有的DIV对象数组中
        CSP_DIVS[oDiv.id] = oDiv;
        
        //添加到menuDiv数组中
        this.menuDivs.push(oDiv);
        
        //如果属于分割线，进行特殊处理
        if(childMenus[i].id.indexOf("menuLine") != -1)
        {
            oDiv.style.height = 10;
            oDiv.style.paddingLeft = "10px";
            oDiv.style.paddingTop = "3px";
            oDiv.style.border = "0px solid";
            oDiv.style.cursor = "hand";
            oDiv.innerHTML = "<img style='height:2px;width:197px' src='" + this.path + "images/Banner2.gif' />";
            this.height += parseInt(oDiv.style.height); 
            oFragment.appendChild(oDiv);
            continue;
        }
        else
        {            
            oDiv.style.height = 25;
            oDiv.style.paddingTop = "3px"; 
            oDiv.style.paddingLeft = "20px";
            oDiv.style.cursor = "hand";
            oDiv.menuObj = childMenus[i];
            oDiv.Obj = this;
            oDiv.path = this.path;
            
            if(childMenus[i].imageUrl != null && childMenus[i].imageUrl.length !==0 &&  childMenus[i].imageUrl != "null")
            {
                oDiv.innerHTML = "<img style='width:16px;height:16px' src='" + this.path + childMenus[i].imageUrl + "' />&nbsp;<span style='font:10pt;width:157px'>"+ childMenus[i].menuName + "</span>";     
            }
            else
            {
                oDiv.innerHTML = "<span style='width:16px;height:16px'></span><span style='font-size:10pt;width:157px'>"+ childMenus[i].menuName + "</span>";     
            }
            
            oDiv.onclick = function(){ this.Obj.menuClick(this.menuObj);};
            oFragment.appendChild(oDiv);
            this.height += parseInt(oDiv.style.height); 
        }
        //添加DIV的移动事件,如果属于系统菜单，只给业务配置台和接续菜单添加子菜单
        /*
        if((this.hasChild(childMenus[i].id)) && 
            ((childMenus[i].parentId == CSP_BSF_SYSMENUID && childMenus[i].id == CSP_SERVICECONFIG) || 
             (childMenus[i].moduleId == CSP_BSF_CALLBARID) ||
             (childMenus[i].parentId == CSP_BSF_TOOLID) ||
             (childMenus[i].parentId != CSP_BSF_SYSMENUID && this.parentId.length > 3)))
        */
        //将所有菜单显示
        if(this.hasChild(childMenus[i].id))
        {
            //添加显示子菜单的箭头图标
            if(this.path != null)
            {
                oDiv.innerHTML = oDiv.innerHTML + "<img src='" + this.path + "images/arrow_cyaneous.gif' style='paddingRight:10'/>";
            }
            else
            {
                oDiv.innerHTML = oDiv.innerHTML + "<img src='images/arrow_cyaneous.gif' style='paddingRight:10'/>";
            }
            
            //显示孩子菜单窗口
            oDiv.onmouseover = function() {
                //创建孩子菜单项
                if(allMenus[this.id] == null)
                {
                    var cMenu = new menu(this.Obj.htmlObj,this.id,this.path+'p');
                }
                
                //显示孩子菜单窗口
                allMenus[this.id].showMenu(this.Obj,this);
                this.style.backgroundColor = "#CC9933";
            }   
            
            //如果有孩子菜单，则点击事件执行显示孩子菜单操作
            oDiv.onclick = oDiv.onmouseover;   
            
            oDiv.onmouseout = function() {
                if(this.isCallBarItem.indexOf("H")>-1)
                {
                    this.style.backgroundColor = "#98B9E7";
                }
                else
                {
                    this.style.backgroundColor = "#EBF0F4";
                }
            }  
        }
        //如果没有孩子菜单，这定义菜单点击事件
        else
        {
            oDiv.onmouseover = function() {
                //隐藏孩子菜单项
                this.style.backgroundColor = "#CC9933";
                var oTmp = this.Obj.popup.document.parentWindow.createPopup();
                oTmp.show(0,0,0,0);
                oTmp.hide();
            }
            
            oDiv.onmouseout = function() {
                if(this.isCallBarItem.indexOf("H")>-1)
                {
                    this.style.backgroundColor = "#98B9E7";
                }
                else
                {
                    this.style.backgroundColor = "#EBF0F4";
                }
            }
            
            oDiv.onclick = function() {
                this.Obj.menuClick(this); 
                if(this.isCallBarItem.indexOf("H")>-1)
                {
                    this.style.backgroundColor = "#98B9E7";
                }
                else
                {
                    this.style.backgroundColor = "#EBF0F4";
                }
                //当前菜单隐藏掉
                var oTmp = window.createPopup();
                oTmp.show(0,0,0,0);
                oTmp.hide();
            }
        }   
    }  
    
    //add by wangjiaqi 解决测试问题，在菜单中使用右键刷新导致菜单不可见，处理方式：屏蔽右键
    this.popup.document.body.oncontextmenu = function(){return false;}
    wClientLog("create div object end");
    this.popup.document.body.appendChild(oFragment);
    this.popup.document.body.style.borderTop = "1px solid #A0B1C1";
    this.popup.document.body.style.borderLeft = "1px solid #A0B1C1";
    this.popup.document.body.style.borderRight = "1px solid #666666";
    this.popup.document.body.style.borderBottom = "1px solid #666666"; 
};

//叶子节点菜单的点击事件
menu.prototype.menuClick = function(divObj) {
    var menuObj = divObj.menuObj;
    menuObj.name = menuObj.menuName;
    
    //记录客户端日志
	var oStringBuffer = new Array();
	oStringBuffer.push("menu leaf click: begin    menuName:");
	oStringBuffer.push(menuObj.menuName);
	oStringBuffer.push(",isCallBarItem:");
	oStringBuffer.push(menuObj.isCallBarItem);
	oStringBuffer.push(",menuUrl:");
	oStringBuffer.push(menuObj.menuUrl);
	wClientLog(oStringBuffer.join(""));
    oStringBuffer = null;
    
    //接续菜单处理事件,剔除呼叫适配菜单
    //if(menuObj.moduleId == CSP_BSF_CALLBARID && menuObj.parentId != CSP_CALLCONF)
    if(menuObj.isCallBarItem == "1")
    {
        wClientLog("do cccEvent begin");
        var oAction = top.publicObject[CSP_CALLBAR];
        if(oAction != null && divObj.menuUrl != "null")
        {
            wClientLog("cccEvent execute begin");
            oAction.eval(divObj.menuUrl);
            wClientLog("cccEvent execute end");
        }
        else
        {
            if(oAction == null)
            {
                wClientLog("oAction is null");
            }
            if(divObj.menuUrl == null)
            {
                wClientLog("menuUrl is null");
            }
        }
        wClientLog("do cccEvent end");
    }
    //需要执行js事件的菜单
    else if(menuObj.isCallBarItem == "2")
    {
        wClientLog("do js event begin");
        if(menuObj.menuUrl != null)
        {
            wClientLog("js event execute begin");
            eval(menuObj.menuUrl);
            wClientLog("js event execute end");
        }
        else
        {
            wClientLog("menuUrl is null");
        }
        //如果是上下班,需要特殊处理菜单事件
        if(menuObj.id  == onDutyId)
        {
            divObj.disabled = true;
            //排好顺序的js菜单下班是上班的下一个,直接修改下个DIV对象的属性，避免循环浪费性能
            divObj.nextSibling.disabled = false;
        }
        if(menuObj.id  == offDutyId)
        {
            divObj.disabled = true;  
            divObj.previousSibling.disabled = false;    
        }
        wClientLog("do js event end");
    }
    //打开系统的菜单
    /*
    else if(menuObj.parentId == CSP_SERVICECONFIG || menuObj.parentId == CSP_BSF_SYSMENUID)
    {
        //根据选择的系统自动收缩分割条
        var oSclipWindow = top.publicObject["sclipWindow"];
        if(menuObj.id  == CSP_SERVICE && oSclipWindow != null)
        {
            oSclipWindow.leftShow();
        }
        else if(oSclipWindow != null)
        {
            oSclipWindow.leftHide();
        }
        //添加子系统菜单
        var oWin = top.publicObject["rightWindow"];
        oWin.appendMenuImage(menuObj);
        
        //添加子系统默认的Tab页签
        var oTabSet = top.publicObject["mainTab"];
        
        //通过menuUrl找到需要添加的子菜单对象
        var tmpMenu = top.publicObject["menu"];
        if(tmpMenu)
        {
            for(var i=0;i<tmpMenu.length;i++)
            {
                if(tmpMenu[i].menuUrl == menuObj.menuUrl && tmpMenu[i] != menuObj)
                {
                     if(oTabSet != null)
                     {
                         oTabSet.appendTab(tmpMenu[i].id,tmpMenu[i].menuName,tmpMenu[i].menuUrl,tmpMenu[i].imageUrl);
                         //将打开的Tab页签添加到工作区管理列表
                         top.addOpenTab(menuObj);
                     }
                     break;
                }
            }
        } 
  
    }
    */
    //其他菜单添加Tab页签操作
    else
    {
        //添加子系统默认的Tab页签
        wClientLog("action event execute begin");
        if(menuObj.menuUrl != null && menuObj.menuUrl != "null" && menuObj.menuUrl.length != 0)
        {
            var oTabSet = top.publicObject["mainTab"];
            if(oTabSet != null)
            {
                oTabSet.appendTab(menuObj.id,menuObj.menuName,menuObj.menuUrl,menuObj.imageUrl);
                //将打开的Tab页签添加到工作区管理列表,暂时不启用
                //top.addOpenTab(menuObj);
            }
            else
            {
                wClientLog("tabSet in topPublic is null");
            }
        }
        wClientLog("action event execute end");
    }
    
    wClientLog("menu leaf click: end");
    wClientLog("show customer info begin");
    
    //执行客户信息显示/隐藏事件
    if(menuObj.isDisplay != null && menuObj.isDisplay == '1')
    {
        //显示操作
        if(top.showLeft != null)
        {
            top.showLeft();
        }
    }
    else if(menuObj.isDisplay != null && menuObj.isDisplay == '0')
    {
        //隐藏操作
        if(top.hideLeft != null)
        {
            top.hideLeft();
        }
    }
    else
    {    
    }
    wClientLog("show customer info end");
}

menu.prototype.moveOver = function() {
};


//判断菜单是否含有孩子
menu.prototype.hasChild = function(id) {
    var oFlag = false;
    var oTmp = top.publicObject["menu"];
    
    if(oTmp)
    {
        for(var i=0;i<oTmp.length;i++)
        {
            if(oTmp[i].parentId == id)
            {
                oFlag = true;
                i = oTmp.length;
            }
        }
    }
   
    return oFlag;
};

//菜单排序函数
menu.prototype.compareFunc = function(value1,value2) {
    if(value1 == null || value2 == null)
    {
        return 0;
    }
    var iNum1 = parseInt(value1.displayNo);
    var iNum2 = parseInt(value2.displayNo);
        
    if(iNum1 > iNum2)
    {
        return 1;
    }
    else if(iNum1 < iNum2)
    {
        return -1;
    }
    else
    {
        return 0;
    }
};

//popup菜单隐藏事件
menu.prototype.hide = function() {
    if(this.popup != null)
    {
        this.popup.hide();
    }
};

//与接续按钮联动需求，在显示接续菜单时，进行刷新状态
menu.prototype.refresh = function() {
};

//获取接续菜单的状态
menu.prototype.getMenuItemStatus = function(btnId) {
    var ret = null;
    var sBtnId = btnId;//btnId 一般类似为“btnSignOut"
    sBtnId = sBtnId.substring(3,sBtnId.length);
    var mothName = "top.publicObject['" + CSP_CALLBAR + "'].controlManager.get" + sBtnId + "Status()";//一般类似为“getSignOutStatus?
    
    ret = top.publicObject[CSP_CALLBAR]
    if(ret == null)
    {
        return ret;
    }
    
    try
    {
        ret = eval(mothName);
    }
    catch(e)
    {
       ret = null; 
    }
    
    return ret;                
};

//刷新接续按钮状态
menu.prototype.refreshStatue = function(divObj) {
    if(divObj != null)
    {
        for(var i=0;i<this.popup.document.body.childNodes.length;i++)
        {
            
            var tmpDiv = this.popup.document.body.childNodes[i];
            
            //如果属于接续父菜单，则可见可用
            if(this.hasChild(this.popup.document.body.childNodes[i].id))
            {
            	tmpDiv.style.display = "block";
                tmpDiv.style.filter = "";
                tmpDiv.disabled = false;
                
                continue;
            }
            
            var statue = this.getMenuItemStatus(this.popup.document.body.childNodes[i].id);
            //可见可用
            if(statue == 2)
            {
                tmpDiv.style.display = "block";
                tmpDiv.style.filter = "";
                tmpDiv.disabled = false;
            }
            //可见不可用?
            else if(statue == 1)
            {
                tmpDiv.style.display = "block";
                tmpDiv.style.filter = "gray";
                tmpDiv.disabled = true;  
            }
            //不可见?
            else if(statue == 0)
            {
                tmpDiv.style.display = "block";
                tmpDiv.style.filter = "gray";
                tmpDiv.disabled = true;  
            }             
            else
            {
                tmpDiv.style.display = "";
                tmpDiv.style.filter = "";
                tmpDiv.disabled = false; 
            } 
        }
    }
};

//刷新新增客户接触菜单状态
menu.prototype.refreshCctMenu = function(divObj){
    
    if (typeof chfApi == "undefined" 
        || chfApi == null || !chfApi.isSignIn())
    {
        return;
    }	
        
    if(divObj != null)
    {
        for(var i=0;i<this.popup.document.body.childNodes.length;i++)
        {
            
            if(this.popup.document.body.childNodes[i].id=="102005")
            {
                var tmpDiv = this.popup.document.body.childNodes[i];
            
                var statue = chfApi.isTalking();
                
                if(statue == true)
                {//可见不可用
                    tmpDiv.style.display = "block";
                    tmpDiv.style.filter = "gray";
                    tmpDiv.disabled = true;  
                }
                else
                {//可见可用
                    tmpDiv.style.display = "block";
                    tmpDiv.style.filter = "";
                    tmpDiv.disabled = false;
                }  
            }     
        }
    }
    
};